﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace LikeProxy.Controls
{
    public partial class ImageToBase64Control : UserControl
    {
        public ImageToBase64Control()
        {
            InitializeComponent();
        }

        private void button_to_base64_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog fileDialog = new OpenFileDialog
                {
                    Filter = "图像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png",
                    ValidateNames = true,
                    CheckPathExists = true,
                    CheckFileExists = true
                };
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    Image image = Image.FromFile(fileDialog.FileName);
                    using (MemoryStream ms1 = new MemoryStream())
                    {
                        image.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                        byte[] arr1 = new byte[ms1.Length];
                        ms1.Position = 0;
                        ms1.Read(arr1, 0, (int)ms1.Length);
                        ms1.Close();
                        textBox_content.Text = "data:image/png;base64," + Convert.ToBase64String(arr1);
                    }

                }
            }
            catch (Exception)
            {

            }

        }

        private void btn_copy_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_content.Text))
            {
                return;
            }
            Clipboard.SetText(textBox_content.Text);
        }
    }
}
